﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace 国寿小画家
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 请求
        /// </summary>
        private HttpClient httpClient;

        private bool IsStop = false;

        private int seqNum = 0;

        private string adress1 = "http://bbs.gxsky.com/portal.php?mod=list&catid=483&page={0}";
        private string adress2 = "http://bbs.gxsky.com/{0}&infloat=yes&handlekey=avt&inajax=1&ajaxtarget=fwin_content_avt";


        private void FrmMain_Load(object sender, EventArgs e)
        {

            httpClient = new HttpClient(new Uri(adress1));
            httpClient.Encoding = Encoding.GetEncoding("gb2312");
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.SetText("开始了....");
            this.IsStop = false;
            Thread thread = new Thread(new ThreadStart(BeginVote));
            thread.Start();
        }

        private void BeginVote()
        {
            //this.IsStop = true;
            while (!this.IsStop)
            {
                try
                {
                    HttpClient client = new HttpClient(new Uri(adress1));

                    //client.Encoding = Encoding.GetEncoding("gb2312");
                    client.Headers.Add("Accept", "");
                    client.Headers.Add("Accept-Language", "zh-cn");
                    client.Headers.Add("Referer", "http://bbs.gxsky.com/portal.php?mod=list&catid=483&page=8");
                    client.Headers.Add("x-requested-with", "XMLHttpRequest");
                    //client.Headers.Add("Accept-Encoding","gzip, deflate");
                    client.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)");
                    string result = "";
                     int currentPage = int.Parse(this.tbCurrentPage.Text.Trim());
                    while (string.IsNullOrEmpty(result) && currentPage>=1)
                    {
                        this.SetText(currentPage+"页");
                        adress1 = "http://bbs.gxsky.com/portal.php?mod=list&catid=483&page={0}";
                        this.tbCurrentPage.Text = currentPage.ToString();
                        adress1 = string.Format(adress1, currentPage);
                        currentPage--;
                        string s = client.DownloadString(adress1);
                        MatchData md = new MatchData("<a id=\"vt_254120\" href=\"/", "\"  onclick=\"showWindow", s);
                        md.Match();
                        md.Substring();
                        result = md.Result;
                    }

                    if (string.IsNullOrEmpty(result))
                    {
                        this.SetText("罗俊江所在 页码错误！请重新输入");
                    }
                    else
                    {
                        result = result.Replace("&amp;", "&");//topic/paint/vote.php?aid=254120&amp;f=0518
                        string srcString = client.DownloadString(string.Format(adress2, result));
                        if (srcString.Contains("投票成功！"))
                        {
                            this.SetText("投票成功！");
                        }
                        else if (srcString.Contains("抱歉！您今天已经为该作品投过票了，请明天再尝试"))
                        {
                            this.SetText("抱歉！您今天已经为该作品投过票了，请明天再尝试。您还可以为其它作品投票!");
                        }
                        else if (srcString.Contains("您提交的信息有误"))
                        {
                            this.SetText("抱歉！您提交的信息有误");
                        }
                        else
                        {
                            this.SetText("未知原因，错误!!!" + srcString);

                        }
                    }
                }
                catch (Exception e)
                {
                    this.SetText("错误了." + e.Message);
                }
                
                if (this.cbChangeIp.Checked)
                {
                    Connect();
                }
            }
            
        }

        /// <summary>
        /// 宽带连接
        /// </summary>
        private void Connect()
        {
            try
            {
                RASDisplay ras = new RASDisplay();
                ras.Disconnect();//断开连接
                int connect = ras.Connect("宽带连接");//重新拨号
                this.SetText("执行连接方法!");
                Uri uriString = new Uri("http://www.baidu.com/");
                byte[] responseData = httpClient.DownloadData(uriString);
            }
            catch (Exception e)
            {
                 this.SetText("宽带连接错误," + e.Message);
            }

        }

        //给值
        /// <summary>
        ///  给值
        /// </summary>
        /// <param name="text"></param>
        private void SetText(string text)
        {
            if (this.rtbResult.InvokeRequired)
            {
                Action<string> set = new Action<string>(SetText);
                this.Invoke(set, text);
            }
            else
            {
                this.rtbResult.AppendText(string.Format("{0}.\t{1}\t{2} {3}", (++seqNum), DateTime.Now, text, Environment.NewLine));
                if (seqNum % 100 == 0)
                {
                    VoteLog.AddResult("MIUI.mi", this.rtbResult.Text);
                    this.rtbResult.Clear();
                }
                this.rtbResult.ScrollToCaret();

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.IsStop = true;
        }


       
    }
}
